import _, { add } from "lodash";

let RKM_option = {

  opt_L: {
    menuName: 'appList',
    axis: { x: null, y: null },
    menulists: [
      {
        fnHandler: 'setTop',
        icoName: 'fa fa-fw fa-arrow-up',
        btnName: '置顶'
      },
      {
        fnHandler: 'setHidden',
        icoName: 'fa fa-fw fa-leaf',
        btnName: '隐藏'
      },
    ],
    tempItem: null,
  },
  opt_M: {
    menuName: 'cusList',
    axis: { x: null, y: null },
    menulists: [
      {
        fnHandler: 'test1',
        icoName: 'fa fa-fw fa-star',
        btnName: '客户详情'
      },
      {
        fnHandler: 'test1',
        icoName: 'fa fa-fw fa-star',
        btnName: '设为星标'
      },
      {
        fnHandler: 'test1',
        icoName: 'fa fa-fw fa-star',
        btnName: '取消星标'
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '分组',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '进度',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '客户资金',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '普客客服类型',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '大客客服类型',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '大客销售类型',
        children: [],
      },
      {
        icoName: 'fa fa-fw fa-star',
        btnName: '风控标签',
        children: [],
      },
    ],
    tempItem: null,
  },
  def: {
    menuName: '',//命名空间，不同属的菜单需要不同命名
    axis: { x: null, y: null },//触发位置，一般不需要额外处理
    menulists: [
      {
        fnHandler: 'setTop', // Binding events(绑定事件)，一般在后面方法中使用同名方法
        icoName: 'fa fa-fw fa-arrow-up', // icon (icon图标 )
        btnName: '置顶' // The name of the menu option (菜单名称，)
      },
    ],
    tempItem: null,// 建议保留做缓存菜单使用对象用
  },//def这个是认作模板使用
}

let RKM_fun = {
  opt_L: {
    showRightMenu: function ($event, item) {
      this.contextMenuData.tempItem = item;
      this.contextMenuData.axis = {
        x: $event.clientX,
        y: $event.clientY
      }
      this.contextMenuData.menulists[0].btnName = `${item.isTop ? '取消' : ''}置顶`;
      this.contextMenuData.menulists[1].btnName = `${item.isHidden ? '' : '取消'}隐藏`;
    },
    setTop: function () {
      let item = this.contextMenuData.tempItem;
      this.$api.setAppTop({
        companyEmployeeId: this.conditional.companyEmployeeId,
        eSCompanyId: item.pid,
        agentid: item.id,
        isTop: !item.isTop
      }).then((res) => {
        this.$store.dispatch('get_App_Cus', { isOutpage: true });
      });
    },
    setHidden: function () {
      let arg = {
        companyEmployeeId: this.conditional.companyEmployeeId,
        eSCompanyId: this.contextMenuData.tempItem.pid,
        agentid: this.contextMenuData.tempItem.id,
      }
      this.$api.setAppHide(arg).then((res) => {
        this.$store.dispatch('get_App_Cus', { isOutpage: true });
      });
    },
  },
  opt_M: {
    getUserInfo: function () {
      this.$api.getUserInfo({
        esId: this.contextMenuData.tempItem.eSCompanyId,
        uId: this.contextMenuData.tempItem.id,
      }).then((res) => {
        this.$store.commit('showCusInfo', {
          x: this.contextMenuData.axis.x,
          y: this.contextMenuData.axis.y,
          obj: res.data.data,
        })
      })
    },
    setStarUser: function () {
      let arg = {
        ceId: this.$store.getters.getCEID(true),
        esId: this.contextMenuData.tempItem.eSCompanyId,
        cusId: this.contextMenuData.tempItem.id,
        isStar: !this.contextMenuData.tempItem.isStar,
      }
      this.$api.setStarUser(arg).then(res => {
        if (res.data.code == 200) {
          this.$store.commit('setCusList_soloAttr', {
            id: arg.cusId,
            key: 'isStar',
            value: arg.isStar
          })
        }
      }).catch(err => { })
    },
    lockUserLabel: function () {
      const [strLock_1, strLock_2, idLock] = ['锁定', '标签锁定', 18];//后端约定，所以特意用了常量
      let constTest = (() => {
        let g = this.selfTags.find((v) => {
          return v.typeName == strLock_1;
        })
        if (g == undefined) {
          return [false, '找不到约定的自定义 锁定 组'];
        }
        if (_.get(g, 'selfTag[0].selfTagId') != idLock) {
          return [false, '找不到约定的自定义 锁定 项id']
        };
        if (_.get(g, 'selfTag[0].tagName') != strLock_2) {
          return [false, '找不到约定的自定义 锁定 项']
        };
        return [true, ''];
      })()
      if (!constTest[0]) {
        throw new Error(constTest[1])
      }
      let isLock = ((that) => {
        let obj = _.get(that, 'selfTag', []).find((v) => {
          return v.typeName == strLock_1
        });
        if (obj == undefined) { return false; }
        if (_.get(obj, 'selfTags[0].selfTagId') != idLock) {
          return false;
        };
        if (_.get(obj, 'selfTags[0].tagName') != strLock_2) {
          return false;
        };
        return true;
      })(this.contextMenuData.tempItem)
      let arg = {
        companyEmployeeId: this.$store.getters.getCEID(true),
        eSCompanyId: this.contextMenuData.tempItem.eSCompanyId,
        userid: this.contextMenuData.tempItem.id,
        selfTagId: idLock
      };
      this.$api.selfTag[isLock ? 'delOne' : 'addOne'](arg).then((res) => {
        if (res.data.code == 200) {
          this.$store.commit('updataSelfTags', {
            esId: arg.eSCompanyId,
            uId: arg.userid,
            tag: {
              pid: 3,
              value: 18,
              label: '标签锁定'
            },
            action: isLock ? '' : 'add',
          })
        }
      })
    },
  }

};


export {
  RKM_option,
  RKM_fun,

}